<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>映射标量为 enum 实例或 null</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="backedenum.from.html">« BackedEnum::from</a></li>
      <li style="float: right;"><a href="reserved.attributes.html">预定义注解 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.backedenum.html">BackedEnum</a></li>
    <li>映射标量为 enum 实例或 null</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="backedenum.tryfrom" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">BackedEnum::tryFrom</h1>
  <p class="verinfo">(PHP 8 &gt;= 8.1.0)</p><p class="refpurpose"><span class="refname">BackedEnum::tryFrom</span> &mdash; <span class="dc-title">映射标量为 enum 实例或 null</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-backedenum.tryfrom-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><strong>BackedEnum::tryFrom</strong></span>(<span class="methodparam"><span class="type"><span class="type">int</span>|<span class="type">string</span></span> <code class="parameter">$value</code></span>): <span class="type"><span class="type">?</span><span class="type"><span class="type static">static</span></span></span></div>

  <p class="para rdfs-comment">
   <span class="methodname"><strong>tryFrom()</strong></span> 方法将 <span class="type">string</span> 和 <span class="type">int</span>
   翻译成对应存在的 enum 条目。
   如果没有找到匹配的条目，返回 null。
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-backedenum.tryfrom-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">value</code></dt>

    <dd>

     <p class="para">
      要映射到枚举条目的标量值。
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-backedenum.tryfrom-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
  该枚举的一个条目实例，未找到时返回 <span class="type">null</span>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-backedenum.tryfrom-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-408">
   <p><strong>示例 #1 基本用法</strong></p>
   <div class="example-contents"><p>
   以下的例子演示了如何返回 enum 条目。
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />enum&nbsp;Suit</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">string<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Hearts&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'H'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Diamonds&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'D'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Clubs&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'C'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Spades&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'S'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$h&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Suit</span><span style="color: #007700">::</span><span style="color: #0000BB">tryFrom</span><span style="color: #007700">(</span><span style="color: #DD0000">'H'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$h</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Suit</span><span style="color: #007700">::</span><span style="color: #0000BB">tryFrom</span><span style="color: #007700">(</span><span style="color: #DD0000">'B'</span><span style="color: #007700">)&nbsp;??&nbsp;</span><span style="color: #0000BB">Suit</span><span style="color: #007700">::</span><span style="color: #0000BB">Spades</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
enum(Suit::Hearts)
enum(Suit::Spades)
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-backedenum.tryfrom-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="unitenum.cases.html" class="methodname" rel="rdfs-seeAlso">UnitEnum::cases()</a> - 生成枚举的条目清单</span></li>
    <li class="member"><span class="methodname"><a href="backedenum.from.html" class="methodname" rel="rdfs-seeAlso">BackedEnum::from()</a> - 映射标量为 enum 实例</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>